<script setup>
import { ref } from 'vue'
import PageContainer from "@/components/PageContainer.vue";
import ChannelSelect from "@/views/article/components/ChannelSelect.vue";
import ArticleEdit from "@/views/article/components/ArticleEdit.vue";
import {Delete, Edit} from "@element-plus/icons-vue";
import {artDelService, artGetListService} from "@/api/article.js";
import { formatTime } from "@/utils/format.js";

const articleList = ref([
  {
    id: 3601,
    title: 'diyidiao',
    state: '已发布',
    cate_name: '体育'
  },
  {
    id: 3602,
    title: '第二条',
    state: '已发布',
    cate_name: '体育'
  },
  {
    id: 3603,
    title: '第三条',
    state: '已发布',
    cate_name: '体育'
  },
  {
    id: 3604,
    title: '第4条',
    state: '已发布',
    cate_name: '体育'
  },
  {
    id: 3605,
    title: '第5条',
    state: '已发布',
    cate_name: '体育'
  },
])
const total = ref(0)   // 总条数

// 定义请求参数对象
const params = ref({
  pagenum: 1,     // 当前页
  pagesize: 10,   // 当前生效的每页条数
  cate_id: '',
  state: ''
})

const getArticleList = async () => {
  const res = await artGetListService(params.value)
  getArticleList.value = res.data.data
  total.value = res.data.total
}
getArticleList()

// 处理分页逻辑
const onSizeChange = (size) => {
  // console.log('当前每页条数', size)
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}
const onCurrentChange = (page) => {
  // console.log('页码变化了', page)
  // 更新当前页
  params.value.pagenum = page
  // 基于最新的当前数据渲染
  getArticleList()
}

// 搜索处理 => 按照最新的条件， 重新检索， 从第一页开始展示
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}

// 重置
const onReset = () => {
  params.value.pagenum = 1    // 重置页面
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

const articleEditRef = ref()
// 添加文章
const onAddArticle = () => {
  articleEditRef.value.open({})
}
// 编辑逻辑
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

// 删除逻辑
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('此操作将研究删除该文章， 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await artDelService(row.id)
  ElMessageBox.success('删除成功')
  await getArticleList()
}

// 添加 或编辑成功的回调
const onSuccess = (type) => {
  if(type === 'add') {
    // 如果是添加， 渲染最后一页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    // 更新成最大页码数，再渲染
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>

    <!-- 表单区域 -->
    <el-form  :inline="true">
      <el-form-item label="文章分类:" style="width: 300px">
        <!--   label 展示给用户看  value 收集起来提交给后台   -->
        <ChannelSelect v-model="params.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态:" style="width: 300px">
        <!--   后台标记发布状态 就是用中文  已发布 / 草稿     -->
        <el-select v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!--   表格区域     -->
    <el-table :data="articleList">
      <el-table-column label=" 文章标题" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label=" 发布时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date )}}
        </template>
      </el-table-column>
      <el-table-column label=" 状态" prop="state"></el-table-column>
      <!--   利用作用域插槽 row 可以拿到当前行的数据 => v-for 遍历 item    -->
      <el-table-column label=" 操作">
        <template #default="{ row }">
          <el-button type="primary" circle plain :icon="Edit" @click="onEditArticle(row)"></el-button>
          <el-button type="danger" circle plain :icon="Delete" @click="onDeleteArticle(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--   分页区域     -->
    <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 3, 5, 10]"
        :background="true"
        layout="jumper, total, sizes, prev, pager, next"
        :total="total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
        style="margin-top: 20px; justify-content: flex-end"
    />

    <!--   添加编辑的抽屉     -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
  </page-container>
</template>
<style lang="scss" scoped>
</style>
